﻿CREATE PROCEDURE [aspnet].AnyDataInTables
    @TablesToCheck int
AS
BEGIN
    -- Check Membership table if (@TablesToCheck & 1) is set
    IF ((@TablesToCheck & 1) <> 0 AND
        (EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE (TABLE_SCHEMA = N'aspnet' AND TABLE_NAME = 'MembershipUsers'))))
    BEGIN
        IF (EXISTS(SELECT TOP 1 UserId FROM aspnet.Membership))
        BEGIN
            SELECT N'aspnet.Membership'
            RETURN
        END
    END

    -- Check aspnet.Roles table if (@TablesToCheck & 2) is set
    IF ((@TablesToCheck & 2) <> 0  AND
        (EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE (TABLE_SCHEMA = N'aspnet' AND TABLE_NAME = 'Roles'))))
    BEGIN
        IF (EXISTS(SELECT TOP 1 RoleId FROM aspnet.Roles))
        BEGIN
            SELECT N'aspnet.Roles'
            RETURN
        END
    END

    -- Check aspnet.Profile table if (@TablesToCheck & 4) is set
    IF ((@TablesToCheck & 4) <> 0  AND
        (EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE (TABLE_SCHEMA = N'aspnet' AND TABLE_NAME = 'Profiles'))))
    BEGIN
        IF (EXISTS(SELECT TOP 1 UserId FROM aspnet.[Profile]))
        BEGIN
            SELECT N'aspnet.Profile'
            RETURN
        END
    END

    -- Check aspnet.PersonalizationPerUser table if (@TablesToCheck & 8) is set
    IF ((@TablesToCheck & 8) <> 0  AND
        (EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE (TABLE_SCHEMA = N'aspnet' AND TABLE_NAME = 'WebPartState_User'))))
    BEGIN
        IF (EXISTS(SELECT TOP 1 UserId FROM aspnet.PersonalizationPerUser))
        BEGIN
            SELECT N'aspnet.PersonalizationPerUser'
            RETURN
        END
    END

    -- Check aspnet.PersonalizationPerUser table if (@TablesToCheck & 16) is set
    IF ((@TablesToCheck & 16) <> 0  AND
        (EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE (TABLE_SCHEMA = N'aspnet' AND TABLE_NAME = 'WebEvent_LogEvent'))))
    BEGIN
        IF (EXISTS(SELECT TOP 1 * FROM aspnet.WebEvent_Events))
        BEGIN
            SELECT N'aspnet.WebEvent_Events'
            RETURN
        END
    END

    -- Check aspnet.Users table if (@TablesToCheck & 1,2,4 & 8) are all set
    IF ((@TablesToCheck & 1) <> 0 AND
        (@TablesToCheck & 2) <> 0 AND
        (@TablesToCheck & 4) <> 0 AND
        (@TablesToCheck & 8) <> 0 AND
        (@TablesToCheck & 32) <> 0 AND
        (@TablesToCheck & 128) <> 0 AND
        (@TablesToCheck & 256) <> 0 AND
        (@TablesToCheck & 512) <> 0 AND
        (@TablesToCheck & 1024) <> 0)
    BEGIN
        IF (EXISTS(SELECT TOP 1 UserId FROM aspnet.Users))
        BEGIN
            SELECT N'aspnet.Users'
            RETURN
        END
        IF (EXISTS(SELECT TOP 1 ApplicationId FROM aspnet.Applications))
        BEGIN
            SELECT N'aspnet.Applications'
            RETURN
        END
    END
END